//
//  DYYueyuCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import YYText


class DYYueyuCell: SYBaseCell , UITableViewDelegate, UITableViewDataSource {
    
    var isZhankai: Bool = false
    var jingquModel:DYJingquDetailModel!
    var model: DYScenicOrdersModel!
    var tableView: UITableView!
    var bgView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor(hex: 0x367FF6)
        v.layer.cornerRadius = 2
        return v
    }()
    
    var sepView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = Constant.bgViewColor
        return v
    }()
    
    let nameLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoBoldFontSize()
        v.text = "门票预约"
        return v
    }()
    
    
    lazy var chakan: VGBaseTitleImgView = {
        let v: VGBaseTitleImgView  = VGBaseTitleImgView(frame: CGRect.zero, type: .imgRight, showType: .normal)
        v.textLb.font = 14.autoFontSize()
        v.textLb.textColor = UIColor(hex: 0x5B5B5B)
        v.textLb.text = "查看更多"
        v.imageV.sy_name("gengduo_xia")
        v.tapClosure = {[weak self] (index, select) in
            if let `self` = self {
                self.chakanAction()
            }
        }

        return v
    }()
    

    public func configureView(_ model: DYScenicOrdersModel, _ index: Int, _ jingquModel: DYJingquDetailModel) {
        self.model = model
        self.jingquModel = jingquModel
        self.tableView.reloadData()
        if let c = model.ticketVoS?.count, c < 3 {
            chakan.isHidden = true
            chakan.snp.remakeConstraints { (make) in
                make.top.equalTo(tableView.snp.bottom)
                make.centerX.equalToSuperview()
                make.width.equalTo(64.autoWidth())
                make.height.equalTo(0.autoWidth())
                make.bottom.equalToSuperview().offset(-10.autoWidth())
            }
            tableView.snp.makeConstraints { (make) in
                make.top.equalToSuperview().offset(46.autoWidth())
                make.left.equalToSuperview().offset(15.autoWidth())
                make.right.equalToSuperview().offset(-15.autoWidth())
                make.height.equalTo(90.0.autoWidth()*CGFloat(c))
            }
        }
        
    }
    
    override func initViews() {
        
        
        setTableView()
        contentView.addSubview(nameLb)
        contentView.addSubview(bgView)
        contentView.addSubview(self.chakan)
        contentView.addSubview(tableView)
        contentView.addSubview(chakan)
        contentView.addSubview(sepView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(16.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(14.5))
            make.width.equalTo(4.0.autoWidth())
            make.height.equalTo(15.0.autoWidth())
        }
        nameLb.snp.makeConstraints { (make) in
            make.centerY.equalTo(bgView)
            make.left.equalTo(bgView.snp.right).offset(autoWidth(7.0))
            make.right.equalToSuperview().offset(autoWidth(-15.0))
            make.height.equalTo(16.0.autoWidth())
        }
        tableView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(46.autoWidth())
            make.left.equalToSuperview().offset(15.autoWidth())
            make.right.equalToSuperview().offset(-15.autoWidth())
            make.height.equalTo(90.0.autoWidth()*2)
        }
        chakan.snp.makeConstraints { (make) in
            make.top.equalTo(tableView.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(64.autoWidth())
            make.height.equalTo(36.0.autoWidth())
            make.bottom.equalToSuperview().offset(-10.autoWidth())
        }
        sepView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(10.0.autoWidth())
            make.bottom.equalToSuperview()
        }
    }
    
    func setTableView() {
        
        //创建表格视图
        self.tableView = UITableView(frame: CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT-NAV_HEIGHT-BOTTOM_SAFE_HEIGHT), style:.plain)
        self.tableView.separatorStyle = .none
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.isScrollEnabled = false
        self.tableView.backgroundColor = Constant.bgViewColor
        self.tableView.layer.cornerRadius = 5
        self.tableView!.register(DYYueyuSubCell.self, forCellReuseIdentifier: "Cell")
        self.contentView.addSubview(self.tableView!)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let m = model  {
            return m.ticketVoS?.count ?? 0
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: DYYueyuSubCell? = tableView.dequeueReusableCell(withIdentifier: "Cell") as? DYYueyuSubCell
        cell?.selectionStyle = .none
        cell?.currentVC  = self.currentVC
        if let m = model  {
            cell?.configureView(m, indexPath.row, jingquModel)
        }
        return cell!
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    @objc func chakanAction() {
        if isZhankai {
            tableView.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(46.autoWidth())
                make.left.equalToSuperview().offset(15.autoWidth())
                make.right.equalToSuperview().offset(-15.autoWidth())
                    make.height.equalTo(90.0.autoWidth()*2)
            }
            (self.currentVC as! DYJingquDetailViewController).tableView.reloadData()
            chakan.textLb.text = "查看更多"
            chakan.imageV.sy_name("gengduo_xia")
            isZhankai = false
        } else {
            
            tableView.snp.remakeConstraints { (make) in
                make.top.equalToSuperview().offset(46.autoWidth())
                make.left.equalToSuperview().offset(15.autoWidth())
                make.right.equalToSuperview().offset(-15.autoWidth())
                if let m = model  {
                    let c = m.ticketVoS?.count ?? 0
                    make.height.equalTo(90.0.autoWidth()*CGFloat(c))
                }
            }
            (self.currentVC as! DYJingquDetailViewController).tableView.reloadData()
            chakan.textLb.text = "收起"
            chakan.imageV.sy_name("gengduo_shang")
            isZhankai = true
        }
    }
   
    
}

class DYYueyuSubCell: SYBaseCell {
    
    var jingquModel: DYJingquDetailModel!
    var yuyueModel: DYScenicOrdersModel!
    var index: Int!
    let leixingLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 16.autoBoldFontSize()
        return v
    }()
    
    let shijianLb: UILabel = {
        let v:UILabel = UILabel()
        v.textColor = UIColor(hex: 0x00C793)
        v.font = 12.autoFontSize()
        return v
    }()
    
    let numLb: YYLabel = {
        let v:YYLabel = YYLabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 13.autoFontSize()
        
        return v
    }()
    
    let priceLb: YYLabel = {
        let v:YYLabel = YYLabel()
        v.textColor = UIColor(hex: 0x1E1E1E)
        v.font = 17.autoFontSize()
        return v
    }()
    
    let yuyue: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("预约", for: .normal)
        v.setTitleColor(UIColor.white, for: .normal)
        v.titleLabel?.font = 14.autoFontSize()
        v.layer.cornerRadius = 14
        v.backgroundColor = UIColor(hex: 0xFFA02A)
        return v
    }()
    
    let yuyueXuzhi: UIButton = {
        let v: UIButton  = UIButton()
        v.backgroundColor = UIColor.clear
        return v
    }()
    
    var alert : DYYuyueXuzhiAlert!
    
    public func configureView(_ model: DYScenicOrdersModel, _ index: Int, _ jingquModel: DYJingquDetailModel) {
        self.jingquModel = jingquModel
        self.yuyueModel = model
        self.index = index
        leixingLb.text = model.ticketVoS?[index].nameStr
        shijianLb.text = "可约\(model.str ?? "")"
        let showStr = "¥" + (model.ticketVoS?[index].price ?? "") + " 起"
        
        let attString = NSMutableAttributedString(string: showStr)
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: 0, length: 1))
        attString.yy_setFont(10.autoFontSize(), range: NSRange(location: 0, length: 1))
        attString.yy_setColor(UIColor(hex: 0xF55035), range: NSRange(location: 1, length: showStr.count-2))
        attString.yy_setFont(14.autoFontSize(), range: NSRange(location: 1, length: showStr.count-2))
        attString.yy_setColor(UIColor(hex: 0xAFAFAF), range: NSRange(location: showStr.count-1, length: 1))
        attString.yy_setFont(10.autoFontSize(), range: NSRange(location: showStr.count-1, length: 1))
        priceLb.attributedText = attString
        
        let num = model.ticketVoS?[index].sum ?? ""
        let xuzhiStr = "已约\(num)  |  预约须知 >"
        let xuzhiAttString = NSMutableAttributedString(string: xuzhiStr)
        xuzhiAttString.yy_setColor(UIColor(hex: 0x666666), range: NSRange(location: 0, length: xuzhiStr.count-7))
        xuzhiAttString.yy_setColor(UIColor(hex: 0x3C72F4), range: NSRange(location: xuzhiStr.count-7, length: 7))
        numLb.attributedText = xuzhiAttString
        
        alert.configure(jingquModel.orderNotice ?? "")
    }
    
    override func initViews() {
        
        self.backgroundColor = .clear
        
        yuyue.addTarget(self, action: #selector(yuyueAction), for: .touchUpInside)
        
        setAlert()
        yuyueXuzhi.addTarget(self, action: #selector(xuzhiAction), for: .touchUpInside)
        contentView.addSubview(leixingLb)
        contentView.addSubview(shijianLb)
        contentView.addSubview(priceLb)
        contentView.addSubview(numLb)
        numLb.addSubview(yuyueXuzhi)
        contentView.addSubview(yuyue)
        contentView.addSubview(lineView)
        
        leixingLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15.0.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(11.0))
            make.height.equalTo(17.autoWidth())
            make.bottom.equalToSuperview().offset(-58.0.autoWidth())
        }
        shijianLb.snp.makeConstraints { (make) in
            make.top.equalTo(leixingLb.snp.bottom).offset(6.5.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(11.0))
            make.height.equalTo(11.5.autoWidth())
        }
        priceLb.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(29.5.autoWidth())
            make.right.equalToSuperview().offset(autoWidth(-23.0))
            make.height.equalTo(11.5.autoWidth())
        }
        numLb.snp.makeConstraints { (make) in
            make.top.equalTo(shijianLb.snp.bottom).offset(13.5.autoWidth())
            make.left.equalToSuperview().offset(autoWidth(11.0))
            make.height.equalTo(13.5.autoWidth())
        }
        yuyueXuzhi.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        lineView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(0.5.autoWidth())
            make.bottom.equalToSuperview()
        }
        yuyue.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15.0.autoWidth())
            make.width.equalTo(60.0.autoWidth())
            make.height.equalTo(28.0.autoWidth())
            make.bottom.equalToSuperview().offset(-14.0.autoWidth())
        }
    }
    
    @objc func yuyueAction() {
        let vc = DYYuYueViewController(id: yuyueModel.ticketVoS?[index].id ?? "", jingquModel: self.jingquModel, ticketName: yuyueModel.ticketVoS?[index].nameStr ?? "")
        self.currentVC.navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc func xuzhiAction() {
        
        alert.isHidden = !alert.isHidden
    }
    
    func setAlert() {
        
        alert = DYYuyueXuzhiAlert()
        let window = UIApplication.shared.keyWindow
        window?.addSubview(alert)
        alert.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}

